/**
 *  Api开发
 */
import { getMasterCommonPkg } from '@/utils/masterData';
const {
  request,
  consts: {
    environment: {
      apiUrl,
      servePrefix,
      commonPrefix,
      integrationPrefix,
      userApiPrefix,
    },
  },
} = getMasterCommonPkg();

/*********************** 左侧菜单接口 ***********************/

// 获取树形结构菜单
export async function getDirectoryTree(data) {
  const { params } = data;
  return request(
    `${servePrefix}/v1/group/tree/${params.spaceId}/${params.type}`,
    {
      method: 'GET',
      params: {
        workspaceId: params.spaceId,
        businessType: params.type,
      },
    },
    apiUrl,
  );
}

// 查询分组信息 名称---

export async function getGroupName(data) {
  return request(
    `${servePrefix}/v1/group/selBatchGroup`,
    {
      method: 'POST',
      body: JSON.stringify(data),
    },
    apiUrl,
  );
}

// 创建目录
export async function createDirectoryTree(data) {
  return request(
    `${servePrefix}/v1/group`,
    {
      method: 'POST',
      body: JSON.stringify(data.body),
    },
    apiUrl,
  );
}

// 目录重命名
export async function directoryreName(data) {
  return request(
    `${servePrefix}/v1/group`,
    {
      method: 'PUT',
      body: JSON.stringify(data.body),
    },
    apiUrl,
  );
}

// 目录删除
export async function directoryreDelete(data) {
  return request(
    `${servePrefix}/v1/group/${data.params.id}`,
    {
      method: 'DELETE',
    },
    apiUrl,
  );
}

// 移动文件夹
export async function directoryMove(data) {
  return request(
    `${servePrefix}/v1/group`,
    {
      method: 'PUT',
      body: JSON.stringify(data.body),
    },
    apiUrl,
  );
}

// 字典-按照类型查询全部字典

export async function Dictionaries(data) {
  let { type } = data;
  return request(
    `${commonPrefix}/v1/dictionary/type/${type}`,
    {
      method: 'GET',
      params: {},
    },
    apiUrl,
  );
}

// 服务单元-分页查询
export async function ServerGroupList(data) {
  let {
    params: { pageSize, pageNum, dataSourceType, groupId, serveUnitName },
  } = data;
  return request(
    `${servePrefix}/dataServe/v1/serveUnit/list/${pageSize}/${pageNum}`,
    {
      method: 'GET',
      params: {
        dataSourceType,
        groupId,
        serveUnitName,
      },
    },
    apiUrl,
  );
}

// 服务单元-详情查询
export async function ServerSearch(data) {
  const { id } = data;
  return request(
    `${servePrefix}/dataServe/v1/serveUnit/detail/${id}`,
    {
      method: 'GET',
      params: {},
    },
    apiUrl,
  );
}

// 服务单元-删除
export async function serveUnitDelete(data) {
  return request(
    `${servePrefix}/dataServe/v1/serveUnit/${data.id}`,
    {
      method: 'DELETE',
    },
    apiUrl,
  );
}

// 查询树型数据源节点
export async function getDataSourceList(data) {
  return request(
    `${commonPrefix}/v1/metadata/dataSource/nodeList`,
    {
      method: 'GET',
      params: data.params,
    },
    apiUrl,
  );
}

// 获取表信息
export async function getTables(data) {
  console.log(data);
  return request(
    `${commonPrefix}/v1/metadata/table/list/dataSourceId/${data.params.datasourceId}`,
    {
      method: 'GET',
      params: data.params,
    },
    apiUrl,
  );
}

// 获取用户列表
export async function getUserList(data) {
  return request(
    `${userApiPrefix}/v1/account/list/${data.params.pageSize}/${data.params.pageNum}`,
    {
      method: 'GET',
      params: data.params,
    },
    apiUrl,
  );
}

// 服务单元-新建
export async function addServeUnit(data) {
  return request(
    `${servePrefix}/dataServe/v1/serveUnit`,
    {
      method: 'POST',
      body: JSON.stringify(data.body),
    },
    apiUrl,
  );
}

// 服务单元-查询详情
export async function getServeUnitDetail(data) {
  return request(
    `${servePrefix}/dataServe/v1/serveUnit/detail/${data.params.id}`,
    {
      method: 'GET',
      params: data.params,
    },
    apiUrl,
  );
}

// 服务单元-修改
export async function updateServeUnit(data) {
  return request(
    `${servePrefix}/dataServe/v1/serveUnit`,
    {
      method: 'PUT',
      body: JSON.stringify(data.body),
    },
    apiUrl,
  );
}

// 表字段详情
export async function metadataTableDetail(data) {
  return request(
    `${commonPrefix}/v1/metadata/table/detail`,
    {
      method: 'GET',
      params: data.params,
    },
    apiUrl,
  );
}

// 服务单元-发布
export async function serveUnitRelease(data) {
  return request(
    `${servePrefix}/dataServe/v1/serveUnit/release/${data.body.id}`,
    {
      method: 'PUT',
    },
    apiUrl,
  );
}

// 服务单元-下线
export async function serveUnitOffline(data) {
  return request(
    `${servePrefix}/dataServe/v1/serveUnit/offline/${data.body.id}`,
    {
      method: 'PUT',
    },
    apiUrl,
  );
}

// api详情 用户权限 我的权限页面
export async function getApiDetail(data) {
  return request(
    `${servePrefix}/v1/api/detail/${data.params.id}`,
    {
      method: 'GET',
      params: data.params,
    },
    apiUrl,
  );
}

//我的权限 --- 获取服务单元名称 通过api详情

export async function getserveUnit(data) {
  return request(
    `${servePrefix}/dataServe/v1/serveUnit/detail/${data.params.id}`,
    {
      method: 'GET',
      params: data.params,
    },
    apiUrl,
  );
}

// 我的权限 --- api分组名称
// /v1/group/selBatchGroup
export async function selBatchGroup(data) {
  return request(
    `${servePrefix}/v1/group/selBatchGroup`,
    {
      method: 'POST',
      body: JSON.stringify(data.body),
    },
    apiUrl,
  );
}
